package com.ptl.bp.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@MappedSuperclass
// @EntityListeners(AuditingEntityListener.class) // 启用审计监听
public class BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Comment("主键")
    @Schema(title = "对象id", example = "1")
    @TableId(type = IdType.AUTO) // Mybatis主键生成策略, 使用数据库的自增
    private Long id;

    @CreationTimestamp // 自动设置创建时间
    @Column(updatable = false, columnDefinition = "datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间'")
    @Comment("创建时间")
    @Schema(title = "创建时间", example = "2022-02-02 10:10:10")
    private LocalDateTime createTime;

    @UpdateTimestamp // 自动更新修改时间
    @Column(nullable = false, columnDefinition = "datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间'")
    @Comment("修改时间")
    @Schema(title = "修改时间", example = "2022-02-02 10:10:10")
    private LocalDateTime updateTime;

    @CreatedBy // 自动记录创建人
    @Column(updatable = false)
    @Comment("创建者id")
    @Schema(title = "创建者id", example = "1")
    private Long createBy;

    @LastModifiedBy // 自动记录修改人
    @Comment("修改者id")
    @Schema(title = "修改者id", example = "1")
    private Long updateBy;

    // 逻辑删除标记（可选）
    // @Column(nullable = false)
    // @Comment("删除标记（0-未删除，1-已删除）")
    // @Schema(hidden = true)
    // private Boolean delFlag = false;

    // @Comment("逻辑删除标志")
    // @Schema(description = "逻辑删除标志")
    // @Column(name = "is_deleted")
    // @TableLogic
    // private Boolean isDeleted = false;

}